// ************************* 78 character ruler *****************************
//
// Title:       SoftWareDlAndFileTran.c
// Purpose:     A short description of the implementation.
//
// Created on:  2012-4-25 at 15:36:06 by luh.
// Copyright:   dafda. All Rights Reserved.
//

// ************************* 78 character ruler *****************************

#include "RFBB.h"         
#include <utility.h>
#include "stdio.h"
//CALI DIR1 OMP1 FDR1 CLI1 FIL1 DCF1 MOD1 TIM1 DAC1 HWI1 FEC1 MON1 EXT1 CSS1
_SOFTWARE_DL_AND_FILE_TRAN_CMD g_szSoftWareDlAndFileTranCmd[13] ={
        {"\n"  ,"CALI.xml"},
        {"\n"  ,"FileDirectory.xml"},
        {"\n"   ,"LTX_UPF.xml"},
        {"\n"   ,"fdrules.xml"}, 
        {"\n" ,"ClippingConfig.xml"},
        {"\n"  ,"FiltersConfig.xml"},
        {"\n"      ,"DpdConfig.xml"},
        {"\n","ModulatorConfig.xml"},
        {"\n"   ,"TimingConfig.xml"},
        {"\n"      ,"DacConfig.xml"},
        {"\n"      ,"HwiConfig.xml"},
        {"\n" ,"frontEndConfig.xml"},
        {"\n"           ,"cssa.bit"}};

char g_szFlashConfigFileCmd[13][BUFFER_SIZE_128] ={
                                            "flash -u CALI CALI.xml 3",
                                            "flash -u DIR1 FileDirectory.xml",
                                            "flash -u OMP1 LTX_UPF.xml",
                                            "flash -u FDR1 fdrules.xml",
                                            "flash -u CLI1 ClippingConfig.xml",
                                            "flash -u FIL1 FiltersConfig.xml",
                                            "flash -u DCF1 DpdConfig.xml",
                                            "flash -u MOD1 ModulatorConfig.xml",
                                            "flash -u TIM1 TimingConfig.xml",
                                            "flash -u DAC1 DacConfig.xml",
                                            "flash -u HWI1 HwiConfig.xml",
                                            "flash -u FEC1 frontEndConfig.xml",
                                            "flash -u CSS1 cssa.bit"};

/**************************************************************************************************
  Function:        SoftWareDlAndFileTran
  Description:     This test is the tests the lock detect status of all synthesizers.
  Calls:           none
  Called By:       dll
  Table Accessed:  none
  Table Updated:   none
  Input:           none
  Output:          error code
  Return:          none
  Test Case ID:    FAHA_DigTRX_BIST_SYNTH
  Used Command:    g_szCmonSynOfLock
**************************************************************************************************/
void __decldllexp TX_TEST SoftWareDlAndFileTran(tTestData *testData, tTestError *testError)
{

    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;   
    ErrMsg szLogMsg;
    CAObjHandle hStepName = 0;
    char* pszStepName = NULL;
	char* SCRIPT_FILE_PATH = NULL;	//add by Vinson
    char kszShmonAgent[BUFFER_SIZE_256] ={'\0'};   
    char szElementPath[BUFFER_SIZE_256] ={'\0'};
    char szReturnString[BUF_SIZE_32K + 1] = {'\0'};
    double dTestNum = 0.0;
    int  error = 0;
    int  iPassFail = 1;
    int i = 0;
    int iReturn = 0;
    
    //////////////////////////////////////////////////////////////////////////   
    // Resolve StepName 
    tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                                       TS_SeqContextStep, CAVT_OBJHANDLE, 
                                       &hStepName));
    tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                                 &pszStepName));

    sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );

    // Post message to GUI
    tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                       TS_UIMsg_ProgressText, 0.0,
                                       pszStepName, 0, VTRUE));
	
	//Get SCRIPT_FILE_PATH from TestPlan Head    
    sprintf( szElementPath, "%s.PARAMETERS.SCRIPT_FILE_PATH", TESTPLAN_BASE_PATH_EXT );
    tsErrChk(TS_PropertyGetValString (testData->seqContextCVI, &errorInfo, szElementPath, 0, &SCRIPT_FILE_PATH));
	
    //--------------------------------------------------------------------
    // Get Test Parameters from Sequence FileGlobals    

    
    //FTP upload CALI files  
    for(i = 0;i < 13;i++)
    	{
		//Fulfill Struct
		sprintf(kszShmonAgent, "%s\\%s", SCRIPT_FILE_PATH, g_szSoftWareDlAndFileTranCmd[i].cRemoteFileName);
		strcpy(g_szSoftWareDlAndFileTranCmd[i].cLocalFilePath, kszShmonAgent);
			
        sprintf( szLogMsg, STRING_MESSAGE_Ftp_File, g_szSoftWareDlAndFileTranCmd[i].cRemoteFileName);
        ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg )); 
        
		//FTP upload
        iReturn = RfbbInetFTPAutoSend(g_szDUTIP, "", "", g_szSoftWareDlAndFileTranCmd[i].cLocalFilePath, g_szSoftWareDlAndFileTranCmd[i].cRemoteFileName, INET_FTP_FILE_TYPE_BINARY);    
        if(iReturn < 0)
        	{
            sprintf( szLogMsg, STRING_MESSAGE_Ftp_File_Err, g_szDUTIP, InetGetErrorMessage( iReturn ) );
            EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
			
			// Set Invalid flag to FilePath
			strcpy(g_szSoftWareDlAndFileTranCmd[i].cLocalFilePath, "Invalid");
            /*-----------Don't need error message
            goto Error;
			-----------------------------------*/ 
        	}
        Delay(2);
    	}

    //InetTelnetOpen(ShellCommand)
    g_hShellCommand = RfbbInetTelnetOpen (g_szDUTIP, g_iShellCommandPort, 0); 

    //flash Config file
    for(i = 0;i < 13;i++)
    {
		if(strcmp (g_szSoftWareDlAndFileTranCmd[i].cLocalFilePath , "Invalid"))
			{
			sprintf(kszShmonAgent,g_szFlashConfigFileCmd[i]);
        	if(ShellCommandWriteReadUntil(kszShmonAgent,"succeeded", szReturnString,5000,1) != 0) 
        		{
            	sprintf(errMsg, "OseWriteReadUntil() - failed");
            	/*-----------Don't need error message
				iPassFail = 0;
            	error = -1;
            	goto Error;
        		-----------------------------------*/
				Delay(2);
				}
			}
    }
    
Error:
    
#if(ON_OFF != SIMU_VERS) 
    
    if(iPassFail == 1)
    {
        testData->result = PASS;
    }
    else 
    {
        testData->result = FAIL;
    }

    // FREE RESOURCES
    InetTelnetClose(g_hShellCommand);
    CA_DISCARD_OBJ( hStepName );
    CA_FREE( pszStepName );
	CA_FREE( SCRIPT_FILE_PATH );

    // If an error occurred, set the error flag to cause a run-time error in TestStand.
    if(error < 0)
    {
        testError->errorFlag = TRUE;
        testError->errorCode = error;
        testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
    }
#else
    testData->result = PASS;
#endif

   // Clear message from GUI
   TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                 TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);

   return;    
}

/**************************************************************************************************
  Function:        TestdataFileTran
  Description:     This test is the tests the lock detect status of all synthesizers.
  Calls:           none
  Called By:       dll
  Table Accessed:  none
  Table Updated:   none
  Input:           none
  Output:          error code
  Return:          none
  Test Case ID:    FAHA_DigTRX_BIST_SYNTH
  Used Command:    g_szCmonSynOfLock
**************************************************************************************************/
void __decldllexp TX_TEST TestdataFileTran(tTestData *testData, tTestError *testError)
{

    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;   
    ErrMsg szLogMsg;
    CAObjHandle hStepName = 0;
    char* pszStepName = NULL;
	char* SCRIPT_FILE_PATH = NULL;	//add by Vinson 
    char kszShmonAgent[BUFFER_SIZE_256] ={'\0'};   
    char szElementPath[BUFFER_SIZE_256] ={'\0'};
    double dTestNum = 0.0;
    int  error = 0;
    int  iPassFail = 1;
    int i = 0;
    int iReturn = 0;
    
    //////////////////////////////////////////////////////////////////////////   
    // Resolve StepName 
    tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                                       TS_SeqContextStep, CAVT_OBJHANDLE, 
                                       &hStepName));
    tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                                 &pszStepName));

    sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );

    // Post message to GUI
    tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                       TS_UIMsg_ProgressText, 0.0,
                                       pszStepName, 0, VTRUE));

    //--------------------------------------------------------------------
       
	//Get SCRIPT_FILE_PATH from TestPlan Head    
    sprintf( szElementPath, "%s.PARAMETERS.SCRIPT_FILE_PATH", TESTPLAN_BASE_PATH_EXT );
    tsErrChk(TS_PropertyGetValString (testData->seqContextCVI, &errorInfo, szElementPath, 0, &SCRIPT_FILE_PATH));
    // Get Test Parameters from Sequence FileGlobals 
    //--------------------------------------------------------------------
	
	//Fulfill path and file name
	sprintf(kszShmonAgent, "%s\\%s", SCRIPT_FILE_PATH, "testdata.bin");
	
    iReturn = RfbbInetFTPAutoSend(g_szDUTIP, "", "",kszShmonAgent, "testdata.bin", INET_FTP_FILE_TYPE_BINARY);    
    if(iReturn < 0)
    {
        sprintf( szLogMsg, STRING_MESSAGE_Ftp_File_Err, g_szDUTIP, InetGetErrorMessage( iReturn ) );
        EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
        
        goto Error;
    } 

   
Error:
    
#if(ON_OFF != SIMU_VERS) 
    
    if(iPassFail == 1)
    {
        testData->result = PASS;
    }
    else 
    {
        testData->result = FAIL;
    }

    // FREE RESOURCES
    CA_DISCARD_OBJ( hStepName );
    CA_FREE( pszStepName );
	CA_FREE( SCRIPT_FILE_PATH );

    // If an error occurred, set the error flag to cause a run-time error in TestStand.
    if(error < 0)
    {
        testError->errorFlag = TRUE;
        testError->errorCode = error;
        testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
    }
#else
    testData->result = PASS;
#endif

   // Clear message from GUI
   TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                 TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);

   return;    
}   

   



// ************************* 78 character ruler *****************************
//                           End of source file
// **************************************************************************

